package com.eway.viewanimator;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AnimatorSet_Activity extends AppCompatActivity implements View.OnClickListener {
	private Button btnSetStart, btnSetItem1, btnSetItem2, btnSetItem3, btnSetItem4, btnSetItem5;
	private boolean isOpen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animator_set_);


		btnSetStart = (Button) findViewById(R.id.btn_set_start);
		btnSetItem1 = (Button) findViewById(R.id.btn_set_item_1);
		btnSetItem2 = (Button) findViewById(R.id.btn_set_item_2);
		btnSetItem3 = (Button) findViewById(R.id.btn_set_item_3);
		btnSetItem4 = (Button) findViewById(R.id.btn_set_item_4);
		btnSetItem5 = (Button) findViewById(R.id.btn_set_item_5);


		btnSetStart.setOnClickListener(this);
		btnSetItem1.setOnClickListener(this);
		btnSetItem5.setOnClickListener(this);


	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_set_start:
				if (!isOpen) {
					doAnimatorSetOpen();
					isOpen = true;
				} else {
					doAnimatorSetClose();
					isOpen = false;
				}

				break;
			case R.id.btn_set_item_1:
				Toast.makeText(AnimatorSet_Activity.this, "点击了", Toast.LENGTH_SHORT).show();
				break;
			case R.id.btn_set_item_5:
				Toast.makeText(AnimatorSet_Activity.this, "点击了5", Toast.LENGTH_SHORT).show();
				break;
		}
	}


	/**
	 * 打开动画
	 */
	private void doAnimatorSetOpen() {
		doAnimatorOpen(btnSetItem1, 0, 5, 500);
		doAnimatorOpen(btnSetItem2, 1, 5, 500);
		doAnimatorOpen(btnSetItem3, 2, 5, 500);
		doAnimatorOpen(btnSetItem4, 3, 5, 500);
		doAnimatorOpen(btnSetItem5, 4, 5, 500);
	}

	/**
	 * 关闭动画
	 */
	private void doAnimatorSetClose() {
		doAnimatorClose(btnSetItem1, 0, 5, 500);
		doAnimatorClose(btnSetItem2, 1, 5, 500);
		doAnimatorClose(btnSetItem3, 2, 5, 500);
		doAnimatorClose(btnSetItem4, 3, 5, 500);
		doAnimatorClose(btnSetItem5, 4, 5, 500);

//			int rId = getResources().getIdentifier("btn_set_item_" + i, "id", getPackageName());



	}

	/**
	 * 功能：打开菜单
	 *
	 * @param btn
	 * @param index
	 * @param total
	 * @param length
	 */
	private void doAnimatorOpen(Button btn, int index, int total, int length) {

		if (btn.getVisibility() != View.VISIBLE) {
			btn.setVisibility(View.VISIBLE);
		}
		AnimatorSet mAnimatroSet = new AnimatorSet();

		double angle = Math.toRadians(90) / (total - 1) * index; // 角度对应的扇形弧度
		int traslateX = -(int) (Math.sin(angle) * length);
		int traslateY = -(int) (Math.cos(angle) * length);

		ObjectAnimator objectAnimatorTranslateY = ObjectAnimator.ofFloat(btn, "translationY", 0, traslateY);
		ObjectAnimator objectAnimatorTranslateX = ObjectAnimator.ofFloat(btn, "translationX", 0, traslateX);

		ObjectAnimator objectAnimatorScaleX = ObjectAnimator.ofFloat(btn, "scaleX", 0f, 1f);
		ObjectAnimator objectAnimatorScaleY = ObjectAnimator.ofFloat(btn, "scaleY", 0f, 1f);
		ObjectAnimator objectAnimatorAlaph = ObjectAnimator.ofFloat(btn, "alpha", 0f, 1f);

		mAnimatroSet.playTogether(objectAnimatorTranslateX, objectAnimatorTranslateY, objectAnimatorScaleX, objectAnimatorScaleY, objectAnimatorAlaph);

		mAnimatroSet.setDuration(1000);
		mAnimatroSet.start();
	}


	/**
	 * 功能：关闭菜单
	 *
	 * @param btn
	 * @param index
	 * @param total
	 * @param length
	 */
	private void doAnimatorClose( final Button btn, int index, int total, int length) {
		if (btn.getVisibility() != View.VISIBLE) {
			btn.setVisibility(View.VISIBLE);
		}
		AnimatorSet mAnimatroSet = new AnimatorSet();

		double angle = Math.toRadians(90) / (total - 1) * index; // 角度对应的扇形弧度
		int traslateX = -(int) (Math.sin(angle) * length);
		int traslateY = -(int) (Math.cos(angle) * length);

		ObjectAnimator objectAnimatorTranslateY = ObjectAnimator.ofFloat(btn, "translationY", traslateY, 0);
		ObjectAnimator objectAnimatorTranslateX = ObjectAnimator.ofFloat(btn, "translationX", traslateX, 0);

		ObjectAnimator objectAnimatorScaleX = ObjectAnimator.ofFloat(btn, "scaleX", 1f, 0f);
		ObjectAnimator objectAnimatorScaleY = ObjectAnimator.ofFloat(btn, "scaleY", 1f, 0f);
		ObjectAnimator objectAnimatorAlaph = ObjectAnimator.ofFloat(btn, "alpha", 1f, 0f);

		mAnimatroSet.playTogether(objectAnimatorTranslateX, objectAnimatorTranslateY, objectAnimatorScaleX, objectAnimatorScaleY, objectAnimatorAlaph);

		mAnimatroSet.setDuration(1000);
		mAnimatroSet.start();


		mAnimatroSet.addListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {

			}

			@Override
			public void onAnimationEnd(Animator animation) {

				btn.setVisibility(View.GONE);

			}

			@Override
			public void onAnimationCancel(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {

			}
		});

	}


}
